import Base from './base'

function ensureSlash(){
    if(!window.location.hash){ // window.locaiton.href .split('#')
       window.location.hash = '/'; // #/
    }
}
function getHash(){
    return window.location.hash.slice(1); // /
}
class HashHistory extends Base{
    constructor(router){
        super(router)
        ensureSlash();
    }
    push(to){
        window.location.hash = to;
    }
    getCurrentLocation(){
        return getHash();
    }
    setupListener = ()=>{
        window.addEventListener('hashchange',()=>{ // 监听hash值的变化，hash变化后再调用transitionTo方法
            this.transitionTo(getHash())
        })
    }
}
export default HashHistory